﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Program
    {
        //玩家登录模块
        public static bool Login()
        {
            Console.WriteLine("-------登录-------");
            Console.Write("请输入账号：");
            string userName = Console.ReadLine();
            Console.Write("请输入密码：");
            string userPwd = Console.ReadLine();
            if (userName == "admin" && userPwd == "123")
            {
                Console.WriteLine("登录成功！");
                Console.WriteLine("-----------------");
                return true;
            }
            else
            {
                Console.WriteLine("登录失败！");
                Console.WriteLine("------------------");
                return false;
            }
        }
        //初始化英雄信息
        public static Hero[] InitHeros()
        {
            Hero[] heros = new Hero[5];
            //第一个英雄初始化
            heros[0] = new Hero();
            heros[0].Name = "东皇太一";
            heros[0].Desc = "楚人于东方立祠祭祀天神，故称东皇。太一则有多种说法，一指星名，又有称是形容神力无边广大。";
            heros[0].Attack = 43;
            heros[0].Defense = 35;
            heros[0].Level = 1;
            heros[0].TotalMoney = 200;
            //第二个英雄初始化
            heros[1] = new Hero();
            heros[1].Name = "大乔";
            heros[1].Desc = "即小乔的姐姐，江东庐江桥玄的女儿。传说都是国色天香，大乔嫁给了孙策，在《三国志》里有零星的记载。";
            heros[1].Attack = 62;
            heros[1].Defense = 30;
            heros[1].Level = 1;
            //第三个英雄初始化
            heros[2] = new Hero();
            heros[2].Name = "陈咬金";
            heros[2].Desc = "即程知节，本名程咬金。秦王李世民帐下的骁将。";
            heros[2].Attack = 47;
            heros[2].Defense = 37;
            heros[2].Level = 1;
            //第四个英雄初始化
            heros[3] = new Hero();
            heros[3].Name = "赵云";
            heros[3].Desc = "赵云是三国时的蜀汉名将。常山真定人，字子龙。公孙瓒败亡，投奔刘备。赵云一生都效忠于蜀国。";
            heros[3].Attack = 45;
            heros[3].Defense = 32;
            heros[3].Level = 1;
            //第五个英雄初始化
            heros[4] = new Hero();
            heros[4].Name = "孙尚香";
            heros[4].Desc = "刘备的续弦夫人，人称孙夫人，孙尚香只是孙夫人在民间传说中的名字。";
            heros[4].Attack = 58;
            heros[4].Defense = 28;
            heros[4].Level = 1;
            //返回值所有英雄信息
            return heros;
        }
        //显示英雄列表
        public static void ShowHeroList(Hero[] heros)
        {
            Console.WriteLine("英雄列表：");
            for (int i = 0; i < heros.Length; i++)
            {
                Console.WriteLine("{0}.{1}", i + 1, heros[i].Name);
            }
        }
        //显示指定英雄的基本信息
        public static void ShowHeroInfo(Hero hero)
        {
            Console.WriteLine("姓名：" + hero.Name);
            Console.WriteLine("简介:" + hero.Desc);
            Console.WriteLine("等级：{0}，伤害：{1},防御:{2}", hero.Level, HeroAttack(hero), HeroDefense(hero));
        }
        //计算英雄的实际攻击力，英雄实际攻击力计算公式:英雄基础攻击力+英雄等级*0.3*基础攻击力
        public static double HeroAttack(Hero hero)
        {
            return hero.Attack * (1 + hero.Level * 0.3);
        }
        //计算英雄的实际防御力，英雄实际防御力计算公式:英雄基础防御力+英雄等级*0.15*基础防御力
        public static double HeroDefense(Hero hero)
        {
            return hero.Defense * (1 + hero.Level * 0.15);
        }
        //选择英雄模块
        public static Hero SelectHero()
        {
            //初始化英雄信息
            Hero[] heros = InitHeros();
            Console.WriteLine("--------------------");
            Console.WriteLine("       英雄列表      ");
            Console.WriteLine("--------------------");
            //显示英雄名称列表
            ShowHeroList(heros);
            Console.Write("请选择英雄编号，查看英雄属性！");
            int heroIndex = int.Parse(Console.ReadLine());
            Hero selectHero;//存储当前选择的英雄
            switch (heroIndex)
            {
                case 1:
                case 2:
                case 3:
                case 4:
                case 5:
                    selectHero = heros[heroIndex - 1];
                    break;
                default:
                    selectHero = heros[0];
                    Console.WriteLine("编号输入错误，默认选中第一个英雄开始游戏");
                    break;
            }
            //显示当前选中的英雄基本信息
            ShowHeroInfo(selectHero);
            return selectHero;
        }
        //初始化装备信息
        public static Equip[] InitEquips()
        {
            Equip[] equips = new Equip[4];
            //初始化第一个装备信息
            equips[0].Name = "铁剑";
            equips[0].Money = 100;
            equips[0].Attack = 15;
            equips[0].Defense = 0;
            //初始化第二个装备信息
            equips[1].Name = "匕首";
            equips[1].Money = 500;
            equips[1].Attack = 25;
            equips[1].Defense = 0;
            //初始化第三个装备信息
            equips[2].Name = "布甲";
            equips[2].Money = 200;
            equips[2].Attack = 0;
            equips[2].Defense = 10;
            //初始化第三个装备信息
            equips[3].Name = "雪山圆盾";
            equips[3].Money = 600;
            equips[3].Attack = 0;
            equips[3].Defense = 20;
            //返回装备信息
            return equips;
        }
        //显示装备列表
        public static void ShowEquipList(Equip[] equips)
        {
            for (int i = 0; i < equips.Length; i++)
            {
                Console.WriteLine("{0}:{1},金币：{2},攻击力:{3},防御力:{4}", i + 1, equips[i].Name, equips[i].Money, equips[i].Attack, equips[i].Defense);

            }
        }
        //购买并装备武器
        public static void BuyAndTakeOnEquip(ref Hero player,Equip buyEquip)
        {
            //购买并装备物品
            if (player.TotalMoney < buyEquip.Money)
            {
                Console.WriteLine("您的金币不足，购买失败！");
            }
            else
            {
                //在英雄所拥有的金币中扣除购买装备所需金币数
                player.TotalMoney -= buyEquip.Money;
                Console.WriteLine("{0}购买成功，花费{1}金币！", buyEquip.Name, buyEquip.Money);
                //为英雄装备武器
                //如果是攻击性武器，防御值为0，装备在英雄的武器属性中
                if (buyEquip.Defense == 0)
                    player.AttackEquip = buyEquip;
                //如果是防御性武器，攻击值为0，装备在英雄的防具属性中
                else
                    player.DefenseEquip = buyEquip;
            }
        }
        //购买装备模块
        public static void BuyEquip(ref Hero player)
        {
            Console.WriteLine("----------------");
            Console.WriteLine("------商店------");
            Console.WriteLine("----------------");
            //初始化装备信息
            Equip[] equips = InitEquips();
            //显示装备列表
            ShowEquipList(equips);
            //根据用户输入购买商品
            Console.WriteLine("您拥有{0}个金币。", player.TotalMoney);
            Console.WriteLine("请输入装备对应编号来购买装备。");
            int equipIndex = int.Parse(Console.ReadLine());
            Equip buyEquip;
            switch (equipIndex)
            {
                case 1:
                case 2:
                case 3:
                case 4:
                    buyEquip = equips[equipIndex - 1];
                    break;
                default:
                    buyEquip = equips[0];
                    Console.WriteLine("编号输入错误，默认购买第一个装备进行游戏");
                    break;
            }
            //购买并装备武器
            BuyAndTakeOnEquip(ref player, buyEquip);
        }
        //打怪练级模块
        public static void Fight(ref Hero player)
        {
            //初始化怪物
            Monster monster;
            monster.Name = "荒野小怪";
            monster.Life = 180 + player.Level * 10;
            monster.Attack = 10 + player.Level * 2;
            monster.Defense = 20 + player.Level * 2;
            monster.KillMoney = 50 + player.Level * 10;
            monster.KillExp = 100 + player.Level * 10;
            //英雄攻击怪物，直到怪物死亡
            do
            {
                int demage = (int)HeroAttack(player) + player.AttackEquip.Attack - monster.Defense;
                Console.WriteLine("{0}对{1}产生了{2}伤害！", player.Name, monster.Name, demage);
                monster.Life -= demage;
            } while (monster.Life > 0);
            //杀死怪物，英雄获得金币
            Console.WriteLine("{0}被杀死，获得{1}金币，获得{2}经验", monster.Name, monster.KillMoney, monster.KillExp);
            //获取杀怪金币和经验值
            player.TotalMoney += monster.KillMoney;
            player.TotalExp += monster.KillExp;
            //判断是否升级，每个等级所需经验值500
            if (player.TotalExp > 500)
            {
                player.Level++;
                player.TotalExp -= 500;
                Console.WriteLine("恭喜你，升级成功，当前等级：{0}", player.Level);
            }
            Console.WriteLine("英雄{0}：金币数：{1}，等级：{2}，经验值：{3}", player.Name, player.TotalMoney, player.Level, player.TotalExp);
        }
        //主函数
        static void Main(string[] args)
        {
            Console.WriteLine("--------------------");
            Console.WriteLine("       王者荣耀      ");
            Console.WriteLine("--------------------");
            //玩家登录
            bool isPassed = false;
            do
            {
                isPassed = Login();

            } while (isPassed == false);
            Console.WriteLine("召唤师，欢迎来到王者荣耀!");
            Console.WriteLine("在这片虚构的王者大陆上，重生的英雄们演绎着新的传奇");
            Console.WriteLine("准备好了吗？精彩对战即将爆发！");
            //选择英雄
            Hero player = SelectHero();
            Console.Write("你已经选择了英雄:" + player.Name + ",");
            Console.WriteLine("下面开始你的王者大陆之旅吧！");
            Console.WriteLine("--------------------");
            Console.WriteLine("-------新手村-------");
            Console.WriteLine("--------------------");
            string isExitGame = "n";
            do
            {
                Console.WriteLine("1.购买装备");
                Console.WriteLine("2.出村练级");
                Console.WriteLine("请输入对应功能编号进行选择。");
                string selectMenu = Console.ReadLine();
                switch (selectMenu)
                {
                    case "1":
                        //购买装备
                        BuyEquip(ref player);
                        break;
                    case "2":
                        //打怪练级
                        Fight(ref player);
                        break;
                }
                Console.WriteLine("是否继续游戏?(y/n)");
                isExitGame = Console.ReadLine();
            } while (isExitGame != "n");
        }
    }
}
